//
//  WCPickerView.m
//  WCN
//
//  Created by 杨鑫 on 15/6/8.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCPickerView.h"
#import "WCMaskView.h"
#import "Macros.h"

@interface WCPickerView () <UIPickerViewDataSource, UIPickerViewDelegate>

@property (weak, nonatomic) WCMaskView *maskView;

@end

@implementation WCPickerView

- (instancetype)init
{
    return [self initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 260)];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 260)];
    if (self) {
        _numberOfComponents = 1;
        
        [self loadSubView];
    }
    return self;
}

- (void)loadSubView
{
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
    toolbar.barTintColor = [UIColor whiteColor];
    
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(leftBtnTap)];
    leftBtn.tintColor = kTxtColor;
    
    UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    UIBarButtonItem *rigthBtn = [[UIBarButtonItem alloc] initWithTitle:@"确认" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnTap)];
    rigthBtn.tintColor = kTxtColor;
    
    [toolbar setItems:@[leftBtn, flex, rigthBtn]];
    
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(66, 0, kScreenWidth-132, 44)];
    self.titleLabel = dateLabel;
    dateLabel.textAlignment = NSTextAlignmentCenter;
    dateLabel.font = [UIFont boldSystemFontOfSize:20.0];
    
    [toolbar addSubview:dateLabel];
    
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, kScreenWidth, 216)];
    self.picker = pickerView;
    pickerView.dataSource = self;
    pickerView.delegate = self;
    pickerView.backgroundColor = kAppBg;
    
    [self addSubview:toolbar];
    [self addSubview:pickerView];
}

- (void)leftBtnTap
{
    [self hide:nil];
}

- (void)rightBtnTap
{
    if (self.close) {
        if (self.pickerDataSource.count>0) {
            self.close([self.picker selectedRowInComponent:0]);
        }
    }
    [self hide:self.save];
}

- (void)show:(UIViewController *)vc completion:(void (^)(BOOL finished))completion
{
    //蒙板
    WCMaskView *maskView = [[WCMaskView alloc] init];
    self.maskView = maskView;
    
    [maskView addSubview:self];
    [vc.view addSubview:self.maskView];
    
    __weak UIView *weakSelf = self;
    [UIView animateWithDuration:.25 delay:.0 options:7<<16 animations:^{
        weakSelf.frame = CGRectMake(0, kScreenHeight-260, kScreenWidth, 260);
    } completion:completion];
}

- (void)hide:(void (^)(BOOL finished))completion
{
    __weak UIView *weakSelf = self;
    [UIView animateWithDuration:.25 delay:.0 options:7<<16 animations:^{
        weakSelf.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 260);
    } completion:^(BOOL finished){
        if (completion) {
            completion(finished);
        }
        [weakSelf.maskView removeFromSuperview];
    }];
}

#pragma mark - <UIPickerViewDataSource, UIPickerViewDelegate>

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return self.numberOfComponents;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return self.pickerDataSource.count;
    } else if (component == 1) {
        return self.secondPickerDataSource.count;
    }
    
    return 0;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = [[UILabel alloc] init];
    label.textAlignment = NSTextAlignmentCenter;
    if (self.numberOfComponents == 1) {
        label.font = [UIFont systemFontOfSize:16.0];
    } else if (self.numberOfComponents == 2) {
        label.font = [UIFont systemFontOfSize:14.0];
    }
    
    if (component == 0) {
        if (self.showKey) {
            label.text = self.pickerDataSource[row][self.showKey];
        } else {
            id showObject = self.pickerDataSource[row];
            if ([showObject isKindOfClass:[NSString class]]) {
                label.text = showObject;
            } else {
                label.text = [showObject valueForKey:@"name"];
            }
        }
    } else if (component == 1) {
        if (self.secondShowKey) {
            label.text = self.secondPickerDataSource[row][self.secondShowKey];
        } else {
            id showObject = self.secondPickerDataSource[row];
            if ([showObject isKindOfClass:[NSString class]]) {
                label.text = showObject;
            } else {
                label.text = [showObject valueForKey:@"name"];
            }
        }
    }
    
    return label;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (self.numberOfComponents == 2) {
        if (component == 0) {
            NSDictionary *firstLevel = self.pickerDataSource[row];
            self.secondPickerDataSource = self.secondDataSource[firstLevel[@"id"]];
            
            [self.picker reloadComponent:1];
        }
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (self.numberOfComponents == 1) {
        return kScreenWidth;
    } else if (self.numberOfComponents == 2) {
        if (component == 0) {
            return kScreenWidth*.3;
        } else {
            return kScreenWidth-kScreenWidth*.3;
        }
    }
    
    return 0;
}

@end
